﻿/**
 * @Description 基础框架服务调用实例-统计图表服务
 *@Author  guoyanfei
 */
var serviceUrl = '/gdp'
var statisticServices = statisticServices || {};
statisticServices.prototype = {
    inti: function () {
        statisticServices.prototype.setSearchColumn();
        statisticServices.prototype.eventBtnSelect();
        statisticServices.prototype.statisticsInitialize();
        statisticServices.prototype.dataView();
    },
    statisticsInitialize:function(){//统计图初始化
        statisticServices.prototype.zhuzhuang();
        statisticServices.prototype.zhexian();
        statisticServices.prototype.bingtu();
    },
    getData:function(){//获取统计图数据
        var xydata =[];
        $.ajax({
            url: serviceUrl + '/business/censusTab/getCensusTableing',
            type: 'get',
            async: false,
            data: { serviceID: serviceId, conQuery: statisticServices.prototype.setSearchParams()},
            dataType: "json",
            success: function (data) {
                xydata = data.data;
            },
            error: function () {
                bootbox.alert("数据加载失败!");
            }
        });
        return xydata;
    },
    getxyTitle:function(){//查询标题
        var xytitle = [];
        $.ajax({
            url: serviceUrl + '/business/censusTab/selectXYMap',
            type: 'get',
            async: false,
            data: { serviceID: serviceId},
            dataType: "json",
            success: function (data) {
                xytitle = data.data;
            },
            error: function () {
                bootbox.alert("标题数据加载失败!");
            }
        });
        return xytitle;
    },
    setSearchColumn: function () {//设置查询列
        var columnData = new Array();
        $.ajax({
            url: serviceUrl + '/business/censusTab/getTableColumnBiao',
            type: 'get',
            async: false,
            data: { serviceID: serviceId },
            dataType: "json",
            success: function (data) {
                columnData = data.data;
            },
            error: function () {
                bootbox.alert("查询条件数据加载失败!");
            }
        });
        if (columnData != null) {
            $.each(columnData, function (i, e) {
                switch (e.showtype) {
                    case 0://文本框
                        $("#txtSearch").append("<label style='margin-left:10px;' for='search_" + e.fieldname + "'>" + e.fieldnamecn + "：</label>");
                        $("#txtSearch").append("<input id='search_" + e.fieldname + "' placeholder='请输入" + e.fieldnamecn + "' type='text' class='form-control' >");
                        break;
                    case 1://下拉框
                        $("#txtSearch").append("<label style='margin-left:10px;' for='search_" + e.fieldname + "'>" + e.fieldnamecn + "：</label>");
                        var strtxt = "<select id='search_" + e.fieldname + "'  class='form-control' >"
                                       + "<option value=''>请选择</option>";
                        if (e.enumvalue != null && e.enumvalue != "") {
                            var enums = e.enumvalue.split(',');
                            for (var i = 0; i < enums.length; i++) {
                                var eb = enums[i].split(':');
                                strtxt += "<option value='" + eb[0] + "'>" + eb[1] + "</option>";
                            }
                        }
                        strtxt += "</select>";
                        $("#txtSearch").append(strtxt);
                        break;
                    case 4://日期框
                        $("#txtSearch").append("<label style='margin-left:10px;' for='search_" + e.fieldname + "'>" + e.fieldnamecn + "：</label>");
                        $("#txtSearch").append("<input id='search_" + e.fieldname + "' data-date-format=\"yyyy-mm-dd\" type='text' class='form-control' >");
                        $("#search_" + e.fieldname + "").datetimepicker();
                        break;
                }
            });

        }
    },
    setSearchParams: function () {//设置查询参数
        var searchParams = "";
        var columnData = new Array();
        $.ajax({
            url: serviceUrl + '/business/censusTab/getTableColumnBiao',
            type: 'get',
            async: false,
            data: { serviceID: serviceId },
            dataType: "json",
            success: function (data) {
                columnData = data.data;
            },
            error: function () {
                bootbox.alert("列数据加载失败!");
            }
        });
        if (columnData != null) {
            var strcon = "=";
            $.each(columnData, function (i, e) {
                if ($('#search_' + e.fieldname).val() != null && $('#search_' + e.fieldname).val() != 'undefined' && $('#search_' + e.fieldname).val() != "") {
                    if (searchParams == "") {
                        searchParams = "\"" + e.fieldname + "\":\"" + $('#search_' + e.fieldname).val() + "\"";
                    } else {
                        searchParams += ",\"" + e.fieldname + "\":\"" + $('#search_' + e.fieldname).val() + "\"";
                    }
                }
            });

            searchParams = "{" + searchParams + "}";
        }
        return searchParams;
    },
    eventBtnSelect:function(){//查询
        $("#btn_search").click(function () {
            statisticServices.prototype.statisticsInitialize();
        });
    },
    dataView:function(){//数据预览
        var dataview = statisticServices.prototype.getData();
        dataview = JSON.stringify(dataview);
        $("#tb_data").html(dataview);
    },
    zhuzhuang: function () {//柱状图实例
        var xydata = statisticServices.prototype.getData();
        var xdata = [];
        var ydata = [];
        var xytitle = statisticServices.prototype.getxyTitle();
        var xtitle = xytitle.xcolumn;
        var ytitle = xytitle.ycolumn;
        $.each(xydata, function (i, e) {
            $.each(e, function (k, v) {
                xdata.push(v.XFIELD);
                ydata.push(v.YFIELD);
            });
        });
        var myChart = echarts.init(document.getElementById('zhuzhuang'));
        var option = {
            title: {
                text: '柱状图示例-' + xtitle + ''
            },
            tooltip: {},
            legend: {
                data: [ytitle]
            },
            xAxis: {
                data: xdata
            },
            yAxis: {
                axisLine: {
                    show: true
                },
                axisTick: {
                    show: true
                },
                axisLabel: {
                    textStyle: {
                        color: '#999'
                    }
                }
            },
            series: [{
                name: ytitle,
                type: 'bar',
                data: ydata
            }]
        };
        myChart.setOption(option);
    },
    zhexian: function () {
        var xydata = statisticServices.prototype.getData();
        var xdata = [];
        var ydata = [];
        var xytitle = statisticServices.prototype.getxyTitle();
        var xtitle = xytitle.xcolumn;
        var ytitle = xytitle.ycolumn;
        $.each(xydata, function (i, e) {
            $.each(e, function (k, v) {
                xdata.push(v.XFIELD);
                ydata.push(v.YFIELD);
            });
        });
        var myChart = echarts.init(document.getElementById('zhexian'));
        option = {
            title: {
                text: '折线图示例-' + xtitle + ''
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: [ytitle]
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: xdata
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name: ytitle,
                    type: 'line',
                    data: ydata
                }
            ]
        };
        myChart.setOption(option);
    },
    bingtu: function () {
        var xydata = statisticServices.prototype.getData();
        var xdata = [];
        var ydata = [];
        var xytitle = statisticServices.prototype.getxyTitle();
        var xtitle = xytitle.xcolumn;
        var ytitle = xytitle.ycolumn;
        $.each(xydata, function (i, e) {
            $.each(e, function (k, v) {
                xdata.push(v.XFIELD);
                var per = {};
                per.value = v.YFIELD;
                per.name = v.XFIELD;
                ydata.push(per);
            });
        });
        var myChart = echarts.init(document.getElementById('bingtu'));
        option = {
            title: {
                text: '饼状图示例-' + xtitle + '',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                type: 'scroll',
                orient: 'vertical',
                right: 10,
                top: 20,
                bottom: 20,
                data: xdata
            },
            series: [
                {
                    name: ytitle,
                    type: 'pie',
                    radius: '55%',
                    center: ['40%', '50%'],
                    data: ydata,
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        myChart.setOption(option);
    }
};

//服务初始化
$(function () {
    $("#sp_seviceUrl").append("服务地址：" + serviceUrl + "/business/censusTab/getCensusTableing");
    $("#sp_seviceUrl").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;服务ID：" + serviceId);
    var comm = statisticServices.prototype;
    comm.inti();
})
